#include "StdAfx.h"
#include "UIString.h"
#include "UIUtility.h"

std::string CEGUI_String_string(CEGUI::String * self)
{
	return ScriptBackend::UIUtility::toStdString(*self);
}

CEGUI::String& CEGUI_String_assign(CEGUI::String* self,CEGUI::String& str)
{
	return self->assign(str);
}

CEGUI::String& CEGUI_String_append(CEGUI::String* self,CEGUI::String& str)
{
	return self->append(str);
}

namespace luabind
{
	template <>
	struct default_converter<CEGUI::String>
		: native_converter_base<CEGUI::String>
	{
		static int compute_score(lua_State* L, int index)
		{
			return lua_type(L, index) == LUA_TSTRING ? 0 : -1;
		}

		CEGUI::String from(lua_State* L, int index)
		{
			return ScriptBackend::UIUtility::toCeguiString(lua_tostring(L, index));
		}

		void to(lua_State* L, CEGUI::String const& x)
		{
			lua_pushstring(L, ScriptBackend::UIUtility::toStdString(x).c_str());
		}
	};

	template <>
	struct default_converter<CEGUI::String const&>
		: default_converter<CEGUI::String>
	{};
}

void ScriptBackend::Module_UIString( lua_State* l )
{
	using namespace luabind;
	using namespace CEGUI;
	using namespace ScriptBackend;
	module(l)
		[
				class_<String>("UIString")
				.property("String",&CEGUI_String_string)
				.property("Size",&String::size)
				.def("Append",(String&(String::*)(const String&,String::size_type,String::size_type))&String::append)
				.def("Append",&CEGUI_String_append)
				.def("Assign",(String&(String::*)(const String&,String::size_type,String::size_type))&String::assign)
				.def("Assign",&CEGUI_String_assign)
				.def("Substr",&String::substr)				
				.def(const_self == other<const String&>())								
				,
				def("Text",&UIUtility::toCeguiString)
		];
}